<template>
	<view class="container">
		<view class="header">
			<view class="title">个人中心</view>
		</view>
		
		<view class="user-info">
			<image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'"></image>
			<view class="info">
				<text class="username">{{userInfo.username}}</text>
				<view class="edit-btn" @click="editProfile">
					<text class="iconfont icon-edit"></text>
					<text>编辑资料</text>
				</view>
			</view>
		</view>
		
		<view class="wish-section">
			<view class="section-title">我的心愿</view>
			<scroll-view scroll-y class="wish-list" @refresherrefresh="onRefresh" refresher-enabled>
				<view class="no-data" v-if="wishList.length === 0">
					<text>暂无心愿，快去发布吧！</text>
				</view>
				
				<view v-else>
					<view 
						class="wish-item" 
						v-for="(item, index) in wishList" 
						:key="index"
						@click="viewWishDetail(item.id)"
					>
						<view class="wish-content">
							<text>{{item.content}}</text>
						</view>
						<view class="wish-footer">
							<text class="time">{{formatTime(item.created_at)}}</text>
							<view class="actions">
								<view class="action-btn" @click.stop="editWish(item)">
									<text class="iconfont icon-edit"></text>
								</view>
								<view class="action-btn" @click.stop="deleteWish(item.id)">
									<text class="iconfont icon-delete"></text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
		<view class="logout-btn" @click="logout">
			<text>退出登录</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					username: '',
					avatar: ''
				},
				wishList: [],
				isLoading: false
			}
		},
		onLoad() {
			this.loadUserInfo();
			this.loadWishList();
		},
		methods: {
			loadUserInfo() {
				this.$api.getUserInfo().then(res => {
					this.userInfo = res;
				}).catch(err => {
					uni.showToast({
						title: '获取用户信息失败',
						icon: 'none'
					});
				});
			},
			
			loadWishList() {
				if (this.isLoading) return;
				
				this.isLoading = true;
				this.$api.getMyWishes().then(res => {
					this.wishList = res;
				}).catch(err => {
					uni.showToast({
						title: '获取心愿列表失败',
						icon: 'none'
					});
				}).finally(() => {
					this.isLoading = false;
				});
			},
			
			onRefresh() {
				this.loadWishList();
				setTimeout(() => {
					uni.stopPullDownRefresh();
				}, 1000);
			},
			
			editProfile() {
				uni.navigateTo({
					url: '/pages/edit-profile/edit-profile'
				});
			},
			
			viewWishDetail(id) {
				uni.navigateTo({
					url: `/pages/wish-detail/wish-detail?id=${id}`
				});
			},
			
			editWish(wish) {
				uni.navigateTo({
					url: `/pages/publish/publish?id=${wish.id}`
				});
			},
			
			deleteWish(id) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这条心愿吗？',
					success: (res) => {
						if (res.confirm) {
							this.$api.deleteWish(id).then(() => {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								});
								this.loadWishList();
							}).catch(err => {
								uni.showToast({
									title: '删除失败',
									icon: 'none'
								});
							});
						}
					}
				});
			},
			
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('token');
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}
					}
				});
			},
			
			formatTime(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				return `${year}-${month}-${day} ${hour}:${minute}`;
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: rgba(245, 245, 245, 0.8);
	}
	
	.header {
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.7);
		backdrop-filter: blur(10px);
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		position: sticky;
		top: 0;
		z-index: 10;
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.user-info {
		display: flex;
		align-items: center;
		padding: 40rpx;
		background-color: rgba(255, 255, 255, 0.7);
		backdrop-filter: blur(10px);
		margin: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin-right: 30rpx;
		background-color: #eee;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}
	
	.info {
		flex: 1;
	}
	
	.username {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
		display: block;
	}
	
	.edit-btn {
		display: flex;
		align-items: center;
		color: #666;
		font-size: 28rpx;
	}
	
	.edit-btn .iconfont {
		font-size: 28rpx;
		margin-right: 8rpx;
	}
	
	.wish-section {
		flex: 1;
		margin: 20rpx;
		background-color: rgba(255, 255, 255, 0.7);
		backdrop-filter: blur(10px);
		border-radius: 16rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		overflow: hidden;
	}
	
	.section-title {
		padding: 20rpx 30rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
	}
	
	.wish-list {
		height: calc(100vh - 500rpx);
		padding: 20rpx;
	}
	
	.no-data {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 200rpx;
		color: #999;
		font-size: 28rpx;
	}
	
	.wish-item {
		background-color: rgba(255, 255, 255, 0.7);
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		backdrop-filter: blur(10px);
		border: 1rpx solid rgba(255, 255, 255, 0.2);
		transition: all 0.3s ease;
	}
	
	.wish-item:active {
		transform: scale(0.98);
	}
	
	.wish-content {
		font-size: 32rpx;
		color: #333;
		line-height: 1.5;
		margin-bottom: 20rpx;
	}
	
	.wish-footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.time {
		font-size: 24rpx;
		color: #999;
	}
	
	.actions {
		display: flex;
		gap: 20rpx;
	}
	
	.action-btn {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		background-color: rgba(0, 0, 0, 0.05);
		display: flex;
		align-items: center;
		justify-content: center;
		transition: all 0.3s ease;
	}
	
	.action-btn:active {
		transform: scale(0.9);
	}
	
	.action-btn .iconfont {
		font-size: 32rpx;
		color: #666;
	}
	
	.logout-btn {
		margin: 40rpx;
		height: 80rpx;
		background-color: rgba(255, 255, 255, 0.7);
		backdrop-filter: blur(10px);
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #ff4d4f;
		font-size: 32rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		transition: all 0.3s ease;
	}
	
	.logout-btn:active {
		transform: scale(0.98);
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
</style> 